package welsir.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import welsir.entity.dos.Seeding;
import welsir.entity.dos.SeedingDisease;
import welsir.entity.vo.ResultMessage;
import welsir.entity.vo.SeedingPageVO;
import welsir.entity.vo.SeedingVO;
import welsir.mapper.SeedingDiseaseMapper;
import welsir.mapper.SeedingMapper;
import welsir.security.annotations.Authentication;
import welsir.security.enums.UserEnums;
import welsir.service.ISeedingService;
import welsir.util.MathUtil;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author welsir
 * @date 2023/3/8 0:25
 */
@RestController
@RequestMapping("/seeding")
@Api(value = "木苗Controller",tags = {"木苗接口"})
public class SeedingController {

    @Resource
    private ISeedingService seedingService;

    @Resource
    private SeedingDiseaseMapper seedingDiseaseMapper;

    @Resource
    private SeedingMapper seedingMapper;
    /**
     * @description: 获取所有木苗信息(品种 价格 防虫害描述)
     * @param: []
     * @return: []
     * @author: WELSIR
     */
    @ApiOperation(value = "获取所有木苗信息")
   // @Authentication(role = UserEnums.ADMIN)
    @GetMapping("getAllSeeding/{page}/{size}")
    public ResultMessage<SeedingPageVO> getAllSeeding(
            @PathVariable Integer page,
            @PathVariable Integer size
    ){
        return ResultUtil.data(seedingService.VoList(page,size));
    }


    /**
     * @description:  根据id查询对应木苗
     * @param: [id]
     * @return: [id]
     * @author: WELSIR
     */
    @ApiOperation(value = "根据uid查询木苗信息")
   // @Authentication(role = UserEnums.ADMIN)
    @GetMapping("querySeedingById/{id}")
    public ResultMessage<SeedingVO> querySeedingById(@PathVariable Long id){
        SeedingVO seedingVO = seedingService.queryById(id);

        return ResultUtil.data(seedingVO);
    }
    /**
     * @description:  根据木苗名称查询
     * @param: [name]
     * @return: [name]
     * @author: WELSIR
     */
    @ApiOperation(value = "根据名称查询木苗信息")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("querySeedingByName/{name}")
    public ResultMessage<SeedingVO> querySeedingByName(@PathVariable String name){
        return ResultUtil.data(seedingService.queryByName(name));
    }

    /**
     * @description:  新增木苗信息
     * @param: [seeding]
     * @return: [seeding]
     * @author: WELSIR
     */
    @ApiOperation(value = "新增木苗信息")
    @Authentication(role = UserEnums.ADMIN)
    @PostMapping("insertSeeding")
    public ResultMessage<Integer> insertSeeding(@RequestBody JSONObject jsonObject){
        JSONObject obj = jsonObject.getJSONObject("seed");

        Seeding seeding = new Seeding();
        JsonToSeeding(obj,seeding);
        seeding.setId(((Integer)MathUtil.randomDigitNumber(7)).longValue());
        List<Integer> diseases = (ArrayList<Integer>)jsonObject.get("diseases");
        if(!Objects.isNull(diseases)){
            for (Integer id : diseases) {
                SeedingDisease seedingDisease = new SeedingDisease();
                seedingDisease.setSeedingId(seeding.getId());
                seedingDisease.setDiseaseId(id.longValue());
                seedingDiseaseMapper.insert(seedingDisease);
            }
        }
        return ResultUtil.data(seedingService.insert(seeding));
    }

    /**
     * @description:  根据id删除木苗
     * @param: [id]
     * @return: [id]
     * @author: WELSIR
     */
    @ApiOperation(value = "根据id删除木苗信息")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("deleteSeeding/{id}")
    public ResultMessage<Integer> deleteSeedingById(@PathVariable Long id){
        seedingDiseaseMapper.delete(new QueryWrapper<SeedingDisease>().eq("seeding_id",id));
        return ResultUtil.data(seedingService.deleteById(id));
    }

    /**
     * @description:  修改木苗信息
     * @param: [seeding]
     * @return: [seeding]
     * @author: WELSIR
     */
    @ApiOperation(value = "修改木苗信息")
    @Authentication(role = UserEnums.ADMIN)
    @PostMapping("updateSeeding")
    public ResultMessage<Boolean> updateSeeding(@RequestBody JSONObject jsonObject){
        JSONObject obj = jsonObject.getJSONObject("seed");
        Seeding seeding = new Seeding();
        JsonToSeeding(obj,seeding);
        List<Integer> diseases = (ArrayList<Integer>)jsonObject.get("diseases");
        seedingDiseaseMapper.delete(new QueryWrapper<SeedingDisease>().eq("seeding_id",seeding.getId()));
        if(!Objects.isNull(diseases)){
            for (Integer id : diseases) {
                SeedingDisease seedingDisease = new SeedingDisease();
                seedingDisease.setSeedingId(seeding.getId());
                seedingDisease.setDiseaseId(id.longValue());
                seedingDiseaseMapper.insert(seedingDisease);
            }
        }
        return ResultUtil.data(seedingService.updateById(seeding));
    }

    @ApiOperation(value = "获得花木数量")
    @GetMapping("getSeedingCount")
    public ResultMessage<Integer> getSeedingCount(){
        return ResultUtil.data(seedingService.count());
    }


    //TODO 获取下一个和上一个seeding的接口方法
    @ApiOperation(value = "获取下一个木苗信息")
    @GetMapping("getNextSeeding/{id}")
    public ResultMessage<Seeding> getNextSeeding(@PathVariable Long id){

        QueryWrapper<Seeding> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id",id).orderByAsc("id").last("limit 1");
        Seeding seeding = seedingMapper.selectOne(queryWrapper);
        return ResultUtil.data(seeding);
    }

    @ApiOperation(value = "获取上一个木苗信息")
    @GetMapping("getPreSeeding/{id}")
    public ResultMessage<Seeding> getPreSeeding(@PathVariable Long id){
        QueryWrapper<Seeding> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("id",id).orderByDesc("id").last("limit 1");
        Seeding seeding = seedingMapper.selectOne(queryWrapper);
        return ResultUtil.data(seeding);
    }
    private void JsonToSeeding(JSONObject jsonObject,Seeding seeding){
        seeding.setSeedingDescription(jsonObject.getString("seedingDescription"));
        seeding.setPicture(jsonObject.getString("picture"));
        seeding.setPrice(jsonObject.getFloat("price"));
        seeding.setType(jsonObject.getString("type"));
        seeding.setName(jsonObject.getString("name"));
        seeding.setId(jsonObject.getLong("id"));
    }
}
